require 'fileutils'

USE_STLPORT = true

def run_command(cmdline)
  puts "CMD: #{cmdline}"
  STDOUT.flush
  puts `#{cmdline}`
  fail "[#{cmdline}]" unless $? == 0
end

def build_extension(name, arch)
  mkdir_p $tempdir unless File.exist? $tempdir
  mkdir_p $targetdir unless File.exist? $targetdir

  if ENV['RHO_PLATFORM'] == 'wm'
    run_command("\"#{$vcbuild}\" /M4 #{name}.vcproj \"Release|#{$sdk}\"")
    cp_r File.join($tempdir, "Release", name + ".lib"), $targetdir

  elsif ENV['RHO_PLATFORM'] == 'win32'
    run_command("\"#{$vcbuild}\" /M4 #{name}.vcproj \"Debug|Win32\"")
    cp_r File.join($tempdir, "Debug", name + ".lib"), $targetdir
  end
end

namespace "build" do
  task :config do
    $targetdir = ENV['TARGET_TEMP_DIR']
    raise "TARGET_TEMP_DIR is not set" if $targetdir.nil?
    $tempdir = ENV['TEMP_FILES_DIR']
    raise "TEMP_FILES_DIR is not set" if $tempdir.nil?
    $rootdir = ENV['RHO_ROOT']
    raise "RHO_ROOT is not set" if $rootdir.nil?

    $vcbuild = ENV['VCBUILD']
    raise "VCBUILD is not set" if $vcbuild.nil?
    $sdk = ENV['RHO_VSPROJ_SDK_PLATFORM']
    raise "SDK is not set" if $sdk.nil?
  end

  task :all => :config do
    build_extension('RhoElementsExt', $arch)
  end
end

task :default => "build:all"
